#ifndef COMMON_THREAD_H_
#define COMMON_THREAD_H_
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <sstream>
#include <fstream>
#include <iostream>

using namespace std;

class Thread{

protected:

	pthread_t t;

	static void* run_thread(void * r){
		((Thread*)r)->run();
		return NULL;
	}

	virtual void run() = 0;

	bool seguir;

	bool yaUnio;

public:

	Thread(){
		seguir = true;
		yaUnio = true;
	}	

	void start(){
	   	pthread_attr_t attr;
		pthread_attr_init(&attr);
	   	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
		pthread_create(&t,&attr,run_thread,this);
		yaUnio = false;
	}


	void esperar(){
		if(!yaUnio){
			seguir = false;
			void * r = NULL;
			pthread_join(t,&r);
			//pthread_exit(r);
			yaUnio = true;
		}
	}	

	virtual ~Thread(){
		esperar();
	}

};

#endif
